package com.cn.web.controller.api;

import cn.com.allunion.common.AllUnionResult;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.pojo.Smsrecord;
import cn.com.allunion.sms.pojo.Userbase;
import cn.com.allunion.sms.service.mybatis.SmsrecordService;
import com.alibaba.fastjson.JSON;
import com.cn.web.bean.PageViewResult;
import com.cn.web.bean.SmsrecordResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 短信发送记录前端控制器
 * Created by YangJie on 2016/4/21.
 */
@Controller
@RequestMapping("/smsrecord")
public class SmsrecordController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsrecordController.class) ;

    @Autowired
    SmsrecordService smsrecordService ;

    @Value("${maxResult}")
    int pageSize ;

    /**
     * 查询发送记录
     * @param operatorId 运营商id
     * @param businessId 业务id
     * @param status 发送状态
     * @param phoneNumber 电话号码
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param pageCurrent 当前页码
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(value = "operatorId", required = false) String operatorId,
                        @RequestParam(value = "businessId", required = false) String businessId,
                        @RequestParam(value = "status", required = false) String status,
                        @RequestParam(value = "phoneNumber", required = false) String phoneNumber,
                        @RequestParam(value = "startDate", required = false) Date startDate,
                        @RequestParam(value = "endDate", required = false) Date endDate,
                        @RequestParam("pageCurrent") int pageCurrent, HttpSession session) {

        Userbase userbase = (Userbase) session.getAttribute("user");

        if(LOGGER.isDebugEnabled()) {
            LOGGER.debug(JSON.toJSONString(userbase));
        }

        if (null != userbase) {
            try {
                QueryResult<Smsrecord> queryResult = smsrecordService.findByCriteria(userbase.getUserId(), operatorId, startDate, endDate,
                        phoneNumber, businessId, status, pageCurrent, pageSize);

                PageViewResult<SmsrecordResult> pageViewResult = new PageViewResult<>(pageSize, pageCurrent);
                pageViewResult.setQueryResult(SmsrecordResult.convert(queryResult));

                return AllUnionResult.ok(pageViewResult);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }

            return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "") ;
        }

        return AllUnionResult.build(HttpStatus.UNAUTHORIZED.value(), "访问被拒绝，没有获得授权") ;
    }
}
